home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / gopher / Unix / GopherTools / jughead / jughead.0.9 / searchCmnds.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-24  |  8.9 KB  |  300 lines

  1. /*****************************************************************************
  2.  * File:    searchCmnds.c
  3.  *
  4.  * Author:    Rhett "Jonzy" Jones
  5.  *        jonzy@cc.utah.edu
  6.  *
  7.  * Date:    May 13, 1993
  8.  *
  9.  * Modified:    May 23, 1993 by Rhett "Jonzy" Jones.
  10.  *        Commented out the pathway stuff with #ifdef DO_PATHWAY
  11.  *        because this isn't ready yet.
  12.  *        Merged this routine into jughead.
  13.  *        Modified StillNeed2Parse() such that "?" gets mapped to
  14.  *        "?help".
  15.  *
  16.  *        At the request of doylej@liberty.uc.wlu.edu gave support
  17.  *        for the "range=start-stop what" special command.
  18.  *
  19.  * Description:    This module takes care of some special commands for use
  20.  *        by the jughead search engine.  A special command is defined
  21.  *        as one of the following:
  22.  *            ?all what
  23.  *            ?help [what]
  24.  *            ?limit=n what
  25.  *            ?version [what]
  26.  *            ?range=start-stop what
  27.  *            ?pathway what        NOT supported yet.
  28.  *        where 'what' is a standard search string, anything enclosed
  29.  *        in square brackets is optional, and all special commands must
  30.  *        be preceeded with '?'.
  31.  *
  32.  *        The pathway command is currently not supported.
  33.  *
  34.  * Routines:    static    short    StillNeed2Parse(char *string,short *theCommand,
  35.  *                        long *limit,long *rangeStart,
  36.  *                        long *rangeEnd);
  37.  *        static    int    HandleSpecialCommand(char *string,
  38.  *                             short theCommand);
  39.  *            char    *SpecialCommand(char *what2find,long *limit);
  40.  *
  41.  * Bugs:    No known bugs.
  42.  *
  43.  * Copyright:    Copyright 1993, University of Utah Computer Center.
  44.  *        This source may be freely distributed as long as this copyright
  45.  *         notice remains intact, and is in no way used for any monetary
  46.  *         gain, by any institution, business, person, or persons.
  47.  *
  48.  ****************************************************************************/
  49.  
  50. #include <ctype.h>
  51. #include <stdio.h>
  52. #include <string.h>
  53.  
  54. #define NIL        (void *)0
  55.  
  56. #define RANGEERROR    -4    /* Error with range usage. */
  57. #define PATHWAYERROR    -3    /* Error with pathway usage. */
  58. #define USAGERROR    -2    /* Generic usage error. */
  59. #define LIMITERROR    -1    /* Error with limit usage. */
  60.  
  61. /* Various jughead commands followed by syntax. */
  62. #define ALLCMND        1    /* ?all what */
  63. #define HELPCMND    2    /* ?help [what] */
  64. #define LIMITCMND    3    /* ?limit=n what */
  65. #define VERSIONCMND    4    /* ?version [what] */
  66. #define RANGECMND    5    /* ?range=start-end what */
  67. #ifdef DO_PATHWAY
  68. #define PATHWAYCMND    5    /* ?pathway what */
  69. #endif
  70.  
  71. /* This variable lists the special commands for use by the jughead search engine. */
  72. static char    *specialCmnds[] = { "all",    /* Return all entries in excess of 1024. */
  73.                     "help",    /* Return a link to a help file. */
  74.                     "limit",    /* Return at most the first n entries. */
  75.                     "version",    /* Return the version of jughead. */
  76.                     "range",    /* Return a range of items.*/
  77. #ifdef DO_PATHWAY
  78.                     "pathway",    /* Return the pathway for a selection. */
  79. #endif
  80.                     0 };
  81. #ifdef DO_PATHWAY
  82. char        *pathString;            /* The selStr to find the path of. */
  83. #endif
  84.  
  85. extern int SendString();    /* defined in "sockets.c". */
  86.  
  87. /*****************************************************************************
  88.  * StillNeed2Parse determines if we have a special command and if so assigns
  89.  * the appropriate command to 'theCommand', which includes any command errors
  90.  * encountered.  This routine returns true if there is still information to
  91.  * parse, and false otherwise.
  92.  ****************************************************************************/
  93. static short StillNeed2Parse(string,theCommand,limit,rangeStart,rangeEnd)
  94.     char    *string;        /* The line of text given. */
  95.     short    *theCommand;        /* Do we have a special command? */
  96.     long    *limit,            /* The number of items to return. */
  97.         *rangeStart,        /* The start of a range. */
  98.         *rangeEnd;        /* The end of a range. */
  99. {    char    *str,            /* Position in 'string'. */
  100.         **s,            /* The special commands. */
  101.         theSpecialCmnd[12];    /* The special command the user issued. */
  102.     short    i;            /* A loop counter. */
  103.  
  104.     /* Initialize some variables. */
  105.     *rangeStart = *rangeEnd = *theCommand = 0;
  106.     theSpecialCmnd[0] = '\0';
  107.  
  108.     /* Skip any whitespace. */
  109.     for (str = string; *str && isspace(*str); str++);
  110.  
  111.     if (*str == '?')        /* Looks like a special command. */
  112.         {
  113.         /* Get a copy of the special command. */
  114.         if (!strcmp(str,"?"))    /* Treat it like ?help. */
  115.             strcpy(theSpecialCmnd,"help");
  116.         else
  117.             {
  118.             for (++str, i = 0; i < 12 && isalpha(*str); i++, str++)
  119.                 theSpecialCmnd[i] = *str;
  120.             theSpecialCmnd[i] = '\0';
  121.             }
  122.  
  123.         /* Locate the special command we found. */
  124.         for (*theCommand = 1, s = specialCmnds; *s; s++, ++*theCommand)
  125.             if (!strcmp(*s,theSpecialCmnd))
  126.                 break;
  127.  
  128.         switch (*theCommand)
  129.             {
  130.             case ALLCMND:
  131.                 *limit = -1;
  132.                 break;
  133.             case HELPCMND:
  134.                 break;
  135.             case LIMITCMND:
  136.                 *limit = 0;
  137.                 if (*str == '=' && isdigit(*++str))
  138.                     for (*limit = 0; isdigit(*str); str++)
  139.                         *limit = *limit * 10 + *str - '0';
  140.                 else
  141.                     *theCommand = LIMITERROR;
  142.                 break;
  143.             case VERSIONCMND:
  144.                 break;
  145.             case RANGECMND:
  146.                 if (*str == '=' && isdigit(*++str))
  147.                     {
  148.                     for (*rangeStart = 0; isdigit(*str); str++)
  149.                         *rangeStart = *rangeStart * 10 + *str - '0';
  150.                     if (*str == '-')
  151.                         for (++str, *rangeEnd = 0; isdigit(*str); str++)
  152.                             *rangeEnd = *rangeEnd * 10 + *str - '0';
  153.                     else
  154.                         {
  155.                         *rangeStart = *rangeEnd = 0;
  156.                         *theCommand = RANGEERROR;
  157.                         }
  158.                     }
  159.                 else
  160.                     *theCommand = RANGEERROR;
  161.                 break;
  162. #ifdef DO_PATHWAY
  163.             case PATHWAYCMND:
  164.                 if (!*str)
  165.                     {
  166.                     *theCommand = PATHWAYERROR;
  167.                     break;
  168.                     }
  169.                 if (*str)    /* Skip any whitespace. */
  170.                     for (++str; isspace(*str); str++);
  171.  
  172.                 /* Extract the pathString. */
  173.                 if (*str == '"')
  174.                     {
  175.                     for (pathString = ++str; *str && *str != '"'; str++);
  176.                     if (*str == '"')
  177.                         *str++ = '\0';
  178.                     else
  179.                         {
  180.                         *theCommand = PATHWAYERROR;
  181.                         break;
  182.                         }
  183.                     }
  184.                 else
  185.                     {
  186.                     for (pathString = str; *str && !isspace(*str); str++);
  187.                     *str = '\0';
  188.                     }
  189.                 break;
  190. #endif
  191.             default:    /* Oops we're in deep shit. */
  192.                 *theCommand = USAGERROR;
  193.                 theSpecialCmnd[0] = '\0';
  194.                 break;
  195.             }
  196.  
  197.         /* Shft the special command out from 'string'. */
  198.         if (*str)
  199.             for (++str; isspace(*str); str++);    /* Skip any whitespace. */
  200.         for (i = 0; *str; i++, str++)
  201.             string[i] = *str;
  202.         string[i] = '\0';
  203.         }
  204.  
  205.     return(*string);
  206.  
  207. }    /* StillNeed2Parse */
  208.  
  209. /*****************************************************************************
  210.  * HandleSpecialCommand returns true if 'theCommand' is an error or we
  211.  * discover an error, and send the information back to the client following
  212.  * gopher protocol.  Otherwise this routine returns false and sets some
  213.  * variables or accomplishes the special command.
  214.  ****************************************************************************/
  215. static int HandleSpecialCommand(string,theCommand)
  216.     char    *string;    /* The remainder of the input line. */
  217.     short    theCommand;    /* The special command or error. */
  218. {    int    error = 0;    /* Did we get an error? */
  219.     char    help[1024];    /* Line of text adhering to gopher protocol. */
  220.  
  221.     switch (theCommand)
  222.         {
  223.         case RANGEERROR:
  224.             sprintf(help,"0range usage error%s",JUGHEADHELP);
  225.             error = SendString(help);
  226.             break;
  227.         case PATHWAYERROR:
  228.             sprintf(help,"0pathway usage error%s",JUGHEADHELP);
  229.             error = SendString(help);
  230.             break;
  231.         case USAGERROR:
  232.             sprintf(help,"0jughead usage error%s",JUGHEADHELP);
  233.             error = SendString(help);
  234.             break;
  235.         case LIMITERROR:
  236.             sprintf(help,"0limit usage error%s",JUGHEADHELP);
  237.             error = SendString(help);
  238.             break;
  239.         case ALLCMND:
  240.             if (!*string)
  241.                 {
  242.                 sprintf(help,"0all usage error%s",JUGHEADHELP);
  243.                 error = SendString(help);
  244.                 }
  245.             break;
  246.         case HELPCMND:
  247.             sprintf(help,"0jughead help%s",JUGHEADHELP);
  248.             SendString(help);
  249.             break;
  250.         case LIMITCMND:
  251.             if (!*string)
  252.                 {
  253.                 sprintf(help,"0limit usage error%s",JUGHEADHELP);
  254.                 error = SendString(help);
  255.                 }
  256.             break;
  257.         case VERSIONCMND:
  258.             sprintf(help,"0This version of jughead is %s%s",VERSION,JUGHEADHELP);
  259.             SendString(help);
  260.             break;
  261.         case RANGECMND:
  262.             break;
  263. #ifdef DO_PATHWAY
  264.         case PATHWAYCMND:
  265.             /* exec pathway on pathString. */
  266.             break;
  267. #endif
  268.         }
  269.     return(error);
  270.  
  271. }    /* HandleSpecialCommand */
  272.  
  273. /*****************************************************************************
  274.  * SpecialCommand handles any special command the jughead search engine
  275.  * might get, and returns nil if there is nothing to parse out, and the
  276.  * string to continue parsing if there is any.
  277.  ****************************************************************************/
  278. char *SpecialCommand(what2find,limit,rangeStart,rangeEnd)
  279.     char    *what2find;        /* The input line. */
  280.     long    *limit,            /* The maximum number of items to return. */
  281.         *rangeStart,        /* The start of a range. */
  282.         *rangeEnd;        /* The end of a range. */
  283. {    short    theCommand;        /* Do we have a special command? */
  284.  
  285.     if (StillNeed2Parse(what2find,&theCommand,limit,rangeStart,rangeEnd))
  286.         {
  287.         if (theCommand)
  288.             if (HandleSpecialCommand(what2find,theCommand))
  289.                 return((char *)NIL);
  290.         }
  291.     else if (theCommand)
  292.         {
  293.         (void)HandleSpecialCommand(what2find,theCommand);
  294.         return((char *)NIL);
  295.         }
  296.  
  297.     return(what2find);
  298.  
  299. }    /* SpecialCommand */
  300.